package com.community.common.service;

import com.community.common.config.BizException;
import com.community.common.mapper.BuildingMapper;
import com.community.common.mapper.CommunityMapper;
import com.community.common.mapper.HouseMapper;
import com.community.common.pojo.domain.Building;
import com.community.common.pojo.domain.Community;
import com.community.common.pojo.domain.House;
import com.community.common.util.CommonUtil;
import com.community.common.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 相机表 服务实现类
 * </p>
 *
 * @author superman
 * @since 2020-08-02
 */
@Slf4j
@Service
public class ExcelService {

    @Autowired
    CommunityMapper communityMapper;
    @Autowired
    CommunityService communityService;
    @Autowired
    BuildingMapper buildingMapper;
    @Autowired
    HouseMapper houseMapper;

    @Transactional(rollbackFor = Exception.class)
    public void initData() {
        String tableName = "xq.xlsx";
        /**
         * 数据校验
         */
        String[] xqColumns = {"xqbm", "xqmc", "xzqh", "gadm", "sqdm", "xxdz", "wymc", "jd", "wd", "zbj", "jyxm", "jybh", "jylxfs"};
        List<Map<String, String>> xqData = ExcelUtil.getData(0, tableName, xqColumns);
        if (CollectionUtils.isEmpty(xqData)) {
            throw new BizException("获取不到小区数据");
        }
        Map<String, String> xqMap = xqData.get(0);
        if (StringUtils.isAnyBlank(xqMap.get("xqmc"), xqMap.get("xzqh"), xqMap.get("gadm"), xqMap.get("sqdm"),
                xqMap.get("xxdz"), xqMap.get("wymc"), xqMap.get("jd"), xqMap.get("wd"), xqMap.get("zbj"), xqMap.get("jyxm"), xqMap.get("jybh"), xqMap.get("jylxfs"))) {
            throw new BizException("小区基础数据不能为空");
        }
        if (communityService.countByName(xqMap.get("xqmc")) > 0) {
            throw new BizException("小区名称不能重复");
        }


        String[] ldDColumns = {"xqbm", "ldh", "dys", "lcs", "jd", "wd"};
        List<Map<String, String>> ldData = ExcelUtil.getData(1, tableName, ldDColumns);
        if (CollectionUtils.isEmpty(ldData)) {
            throw new BizException("获取不到楼栋数据");
        }
        List<String> ldList = new ArrayList<>();
        for (Map<String, String> map : ldData) {
            System.out.println(map.get("ldh"));
            if (StringUtils.isAnyBlank(map.get("ldh"), map.get("dys"), map.get("lcs"), map.get("jd"), map.get("wd"))) {
                throw new BizException("楼栋数据不能为空");
            }
            if (ldList.contains(map.get("ldh").replaceAll("\\.0", ""))) {
                throw new BizException("楼栋号不能重复");
            }
            ldList.add(map.get("ldh").replaceAll("\\.0", ""));
        }

        String[] fwColumns = {"xqbm", "ldh", "dyh", "mph", "zt", "lch"};
        List<Map<String, String>> fwData = ExcelUtil.getData(2, tableName, fwColumns);
        if (CollectionUtils.isEmpty(fwData)) {
            throw new BizException("获取不到房屋数据");
        }
        for (Map<String, String> map : fwData) {
            if (StringUtils.isAnyBlank(map.get("ldh"), map.get("dyh"), map.get("mph"), map.get("zt"), map.get("lch"))) {
                throw new BizException("房屋数据不能为空");
            }
            System.out.println(map.get("ldh") + "-" + map.get("mph"));
            if (!ldList.contains(map.get("ldh").replaceAll("\\.0", ""))) {
                throw new BizException("房屋表，楼栋号不在楼栋表内，楼栋号:" + map.get("ldh"));
            }
        }

        /**
         * 插入数据
         */
        String xqId = CommonUtil.getIdStr();
        communityMapper.insert(Community.builder()
                .id(xqId)
                .name(xqMap.get("xqmc"))
                .place(xqMap.get("xxdz"))
                .createTime(LocalDateTime.now())
                .ssqXzqhdm(xqMap.get("xzqh"))
                .gajgjgdm(xqMap.get("gadm"))
                .sqdm(xqMap.get("sqdm"))
                .wymc(xqMap.get("wymc"))
                .jd(xqMap.get("jd"))
                .wd(xqMap.get("wd"))
                .dtzbj(xqMap.get("zbj"))
                .jyxm(xqMap.get("jyxm"))
                .jybh(xqMap.get("jybh"))
                .jylxfs(xqMap.get("jylxfs"))
                .build());

        Map<String, String> ldRef = new HashMap<>();
        for (Map<String, String> map : ldData) {
            String ldId = CommonUtil.getIdStr();
            ldRef.put(map.get("ldh").replaceAll("\\.0", ""), ldId);
            buildingMapper.insert(Building.builder()
                    .id(ldId)
                    .xqId(xqId)
                    .ldh(map.get("ldh").replaceAll("\\.0", ""))
                    .dys(Integer.valueOf(map.get("dys").replaceAll("\\.0", "")))
                    .lcs(Integer.valueOf(map.get("lcs").replaceAll("\\.0", "")))
                    .jd(map.get("jd"))
                    .wd(map.get("wd"))
                    .build());
        }

        for (Map<String, String> map : fwData) {
            houseMapper.insert(House.builder()
                    .id(CommonUtil.getIdStr())
                    .ldId(ldRef.get(map.get("ldh").replaceAll("\\.0", "")))
                    .dyh(map.get("dyh").replaceAll("\\.0", ""))
                    .mph(map.get("mph").replaceAll("\\.0", ""))
                    .fwztdm("自住".equals(map.get("zt").trim()) ? 2 : 1)
                    .lch(map.get("lch").replaceAll("\\.0", ""))
                    .build());
        }


    }
}

